# -*- coding: UTF-8 -*-
# @Date ：2024/6/27 18:06
# @File：tjkj_get_http_data
# @Author：mmmme
# @IDE：PyCharm
import requests
from src.config.log_config import getLogger

logger = getLogger(__name__)


class TjkjGetHttpData:
    def __init__(self, dual_token: str = None, dual_url: str = None, job_token: str = None, job_url: str = None,
                 location_token: str = None, location_url: str = None):
        self.token = dual_token
        self.job_token = job_token
        self.location_token = location_token
        self.headers = {"Authorization": self.token}
        self.base_url = f"{dual_url}/dual-sys/dual/manually/report"  # http://temp.saas.tjkj.pro:82
        self.data = {"page": 1, "pageSize": 10000, "reportState": "1", "deleted": "0"}
        self.job_url = f"{job_url}/safe-prod/api/common-report/record"  # http://saas.newspo.tjkj.pro:82
        self.location_url = f"{location_url}/person-job/api/reportedUserLocation/List"  # http://saas.newspo.tjkj.pro:82

    def get_risk_analysis_unit(self):
        """
        2.3.1安全风险分析单元
        :return:
        """
        url = f"{self.base_url}/getRiskAnalysis"
        return self.get_data(url, self.headers, self.data)

    def get_risk_event(self):
        """
        安全风险事件
        :return:
        """
        url = f"{self.base_url}/getRiskEvent"
        return self.get_data(url, self.headers, self.data)

    def get_risk_control_measures(self):
        """
        安全风险管控措施
        :return:
        """
        url = f"{self.base_url}/getControlMeasures"
        return self.get_data(url, self.headers, self.data)

    def get_hazard_detection_task(self):
        """
        隐患排查任务
        :return:
        """
        url = f"{self.base_url}/getTaskInformation"
        data = self.data
        result = []
        # 小时制任务
        data["checkCycleHour"] = "0"
        data["checkCycleUnit"] = "0"
        task_records = self.get_data(url, self.headers, data)
        for i in task_records:
            result.append(i)
        # 小时制任务之外任务
        data["checkCycleHour"] = "1"
        data["checkCycleUnit"] = "1"
        task_records1 = self.get_data(url, self.headers, data)
        for ii in task_records1:
            result.append(ii)
        return result

    def get_hazard_detection_record(self):
        """
        隐患排查记录
        :return:
        """
        url = f"{self.base_url}/getTaskRecord"
        result = []
        data = self.data
        # 小时制隐患排查记录
        data.update({"checkCycleHour": "0", "createTimeDay": "0"})
        rev = self.get_data(url, self.headers, data)
        for i in rev:
            result.append(i)
        # 小时制任务之外
        data2 = self.data
        data2.update({"checkCycleHour": "1"})
        rev2 = self.get_data(url, self.headers, data2)
        for ii in rev2:
            result.append(ii)
        return result

    def get_hazard_detection_information(self):
        """
        隐患排查信息
        :return:
        """
        url = f"{self.base_url}/getTaskHidden"
        return self.get_data(url, self.headers, self.data)

    def get_job_ticket_information(self):
        """
        特殊作业票证信息
        :return:
        """
        data = self.data
        data.pop("reportState")
        data["status"] = "1"
        headers = {"Authorization": self.job_token}
        return self.get_data(self.job_url, headers, self.data)

    def get_gas_leak_analysis_alarm(self):
        """
        气体泄漏分析报警
        :return:
        """
        pass

    def get_person_real_time_location(self):
        """
        人员实时定位
        :return:
        """
        # url = "http://saas.newspo.tjkj.pro:82/safe-prod/api/common-report/record"
        data = {"pageNum": 1, "pageSize": 10000,
                "reportedStatCondition": 1,  # 只过滤上报成功的记录
                "reportedState": [1, 2]}
        headers = {"Authorization": self.location_token}
        return self.get_data(self.location_url, headers, data)

    def get_data(self, url, header, data) -> list:
        response = requests.post(url, headers=header, json=data, timeout=60).json()
        if response and response.get("code") == 200:
            if response.get("data").get("records"):
                return response.get("data").get("records")
            else:
                return []
        else:
            logger.error(f"response code {response.get('code')},msg {response.get('msg')}")
            return []
